package com.zhanghe.study.springmvc.controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author zh
 * @date 2021/6/17 18:03
 */
@RestController
public class ModelAttributeController {

    @ModelAttribute
    public void before(@RequestParam("id") String id, Model model){
        model.addAttribute("id",id);
    }


    @ModelAttribute("name")
    public String beforeWith(@RequestParam("name") String name, Model model){
        return name;
    }

    @RequestMapping("/modelAttributeTest")
    public String test(Model model){
        Map<String, Object> stringObjectMap = model.asMap();
        return "id:"+stringObjectMap.get("id")+",name:"+stringObjectMap.get("name");

    }

    @RequestMapping("/modelAttributeTest2")
    public String testParam(Model model,@ModelAttribute("pass") String pass){
        Map<String, Object> stringObjectMap = model.asMap();
        return "id:"+stringObjectMap.get("id")+",name:"+stringObjectMap.get("name")
                +",pass:"+stringObjectMap.get("pass");

    }
}
